package com.ihighest.app.infox.tools;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class CheckNetWork {
	/**
	 * 检查网络是否连接
	 * 
	 * @param context
	 * @return true-连接正常<br>
	 *         false-网络异常
	 */

	public static boolean isNetWorkConnected(Context context) {
		if (context != null) {
			ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo info = manager.getActiveNetworkInfo();
			if (info != null) {
				return info.isAvailable();
			}
		}
		return false;
	}

	/**
	 * 判断WiFi状态
	 * 
	 * @param context
	 * @return true 可用 false不可用
	 */
	public static boolean isWiFiConnected(Context context) {
		if (context != null) {
			ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
			if (info != null) {
				return info.isAvailable();
			}
		}
		return false;
	}

	/**
	 * 判断GPRS状态
	 * 
	 * @param context
	 * @return true 可用 false不可用
	 */
	public static boolean isMobilleConnected(Context context) {
		if (context != null) {
			ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
			if (info != null) {
				return info.isAvailable();
			}
		}
		return false;
	}

	/**
	 * 判断网络状态,并将网络状态返回，true 可用 false 不可用
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isConnected(final Context context) {
		boolean isConnected = false;
		if (isNetWorkConnected(context)) {
			if (isMobilleConnected(context) || isWiFiConnected(context)) {
				isConnected = true;
			} else {
				isConnected = false;
			}
		} else {
			isConnected = false;
		}
		return isConnected;
	}

}
